//
//  SwiftUIView1.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/12/1.
//

import SwiftUI

struct SwiftUIView1: View {
    @State var presetnKey = false
    @State var presetnKey2 = false
    var body: some View {
        VStack {
            NavigationLink(destination: {
                SwiftUIView2(title: "第二个页面push", subtitle: "我要去看看你")
            },  label: {
                Text("页面1")
                    .padding( 10 )
                    .foregroundColor( Color.red )
                    .font( Font.system(size:  20 ) )
            })
            
            Button("点击按钮触发push的效果1") {
                self.presetnKey.toggle()//toggle()可以说是将presetnKey的bool值取反
            }
            
            Button("点击按钮触发push的效果2") {
                self.presetnKey2.toggle()//toggle()可以说是将presetnKey的bool值取反
            }
            
            
            NavigationLink(destination: PresentView(),  label: {
                Text("present切换")
                    .padding( 10 )
                    .foregroundColor( Color.red )
                    .font( Font.system(size:  20 ) )
            })
            .navigationTitle( "页面1" )
            //title 显示样式
            .navigationBarTitleDisplayMode(NavigationBarItem.TitleDisplayMode.inline)
            .sheet(isPresented: $presetnKey) {
                //非全屏模式
                PresentView()
            }
            .sheet(isPresented: $presetnKey2) {
                //非全屏模式
                PresentView2()
            }
            
            
            
        }
       
        
    }
}

struct SwiftUIView1_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView1()
    }
}


//切换presendk
 struct PresentView: View {
    var body: some View {
        Text("present界面切换")
    }
}

struct PresentView_Previews: PreviewProvider {
    static var previews: some View {
        PresentView()
    }
}

//切换presendk
 struct PresentView2: View {
     //用于退出该界面
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        VStack{
            Text("present界面切换")
            Button {
                self.presentationMode.wrappedValue.dismiss()
            } label: {
                Text("点击返回")
            }

        }
        
    }
}

struct PresentView2_Previews: PreviewProvider {
    static var previews: some View {
        PresentView2()
    }
}


struct HomeView: View {
    @StateObject var viewModel = ResourceViewModel()
    @State var searchText = ""
    @State var isEditing = false
    @State var isCommit = false

    var searchView = SearchView()

    var body: some View {
        NavigationView {
            VStack{
                HStack{
                    TextField("搜索", text: $searchText, onCommit: {
                            print(searchText)
                            self.isCommit = true
                        })  // 1
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding(7)
                        .padding(.horizontal, 25)
                        .background(Color(.systemGray6))
                        .cornerRadius(8)
                        .padding(.horizontal, 10)
                        .overlay(
                            HStack {
                                Image(systemName: "magnifyingglass")
                                    .foregroundColor(.gray)
                                    .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                                    .padding(.leading, 16)

                                if isEditing {
                                    Button(action: {
                                        self.searchText = ""
                                    }) {
                                        Image(systemName: "multiply.circle.fill")
                                            .foregroundColor(.gray)
                                            .padding(.trailing, 16)
                                    }
                                }
                            }
                        ) // 2
                        .onTapGesture {
                            self.isEditing = true
                        } // 3
                    NavigationLink(destination:SearchView(searchText: searchText), isActive: $isCommit) {

                    } // 8
                    if isEditing {
                        Button(action: {
                            self.isEditing = false
                            self.searchText = ""
                            // 关闭键盘
                            UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)

                        }) {
                            Text("取消")
                        }
                        .padding(.trailing, 20)
                        .transition(.move(edge: .trailing))
                        .animation(.default)
                    } // 4
                }

                ResourceListView(viewModel: viewModel) // 5
            }.navigationBarHidden(true) // 6
            .onAppear() {
                searchText = ""
                isEditing = false
            } // 7
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}

